# coding=utf-8
# 全世界最帅的帅哥
import time
import hashlib
from lxml import etree
from flask import request
from flask import Flask, make_response    # 这些是本例中所有用到的库


class Message(object):
    def __init__(self, req):
        self.request = req
        self.token = 'x86XVcrKGKi1S0rs2In9'
        self.AppID = 'wx2b9997c03c7af854'
        self.AppSecret = '516607b17a07acba8331f6b61d6ec94f'


class Get(Message):
    def __init__(self, req):
        super(Get, self).__init__(req)
        self.signature = req.args.get('signature')    # 这里分别获取传入的四个参数
        self.timestamp = req.args.get('timestamp')
        self.nonce = req.args.get('nonce')
        self.echostr = req.args.get('echostr')
        self.return_code = 'Invalid'

    def verify(self):
        data = sorted([self.token, self.timestamp, self.nonce])    # 字典排序
        string = ''.join(data).encode('utf-8')    # 拼接成字符串
        hashcode = hashlib.sha1(string).hexdigest()    # sha1加密
        if self.signature == hashcode:
            self.return_code = self.echostr